Partial Class Admin_Countries
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 1, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If DataGrid.Items.Count < 1 Then
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpCode = "COUNTRY"
      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl)

    End If

    Dim Edit_Image As Image = CType(Me.Master.FindControl("L6"), Image)
    Edit_Image.ImageUrl = "~/images/redT.PNG"
    Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Countries_btn"), LinkButton)
    Edit_Button.ForeColor = Drawing.Color.Black
    Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
    Edit_Panel.Visible = True
        SecurePage()

  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    OnMode("Add") : ViewState("mode") = "Add"
    Field_txt.Text = ""
    Field_txt.Focus()
  End Sub

  Private Sub OnMode(ByVal mode As String)

    If mode = "Edit" Then
      'no code
    End If

    DataGrid.Enabled = False
    Add_btn.Enabled = False
    Edit_Panel.Visible = True

  End Sub

  Private Sub NotOnMode()
    DataGrid.Enabled = True
    Add_btn.Enabled = True
    Edit_Panel.Visible = False

  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    NotOnMode()
    ViewState.Remove("mode")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        If ViewState("mode") = "Add" Then
            zClass.LookUpCode = "COUNTRY"
            zClass.ExecQuery("INSERT INTO country_mstr(country_cd, country_desc, " & _
                "country_crea_by, country_crea_dt) VALUES('" & _
                Trim(zClass.MaxCode()) & "'," & UCase(dm.Q(Trim(Field_txt.Text))) & _
                ",'" & Session("UserName") & "','" & Now & "')")

        ElseIf ViewState("mode") = "Edit" Then
            zClass.ExecQuery("UPDATE country_mstr SET " & _
                "country_desc=" & UCase(dm.Q(Trim(Field_txt.Text))) & ", " & _
                "country_last_upd_by='" & Session("UserName") & "', " & _
                "country_last_upd_dt='" & Now & "' " & _
                "WHERE country_cd='" & Trim(ViewState("country_cd").ToString) & "'")

        End If

        zClass.LookUpCode = "COUNTRY"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl)
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("country_cd")
  End Sub

  Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
    If e.CommandName = "Delete" Then
      Dim dm As New DAOMain.CommonDAO
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpCode = "COUNTRY"

      If zClass.OKtoDELETE(Trim(e.Item.Cells(0).Text)) = False Then Exit Sub

      zClass.ExecQuery("DELETE FROM country_mstr WHERE country_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)))

      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl)

    ElseIf e.CommandName = "Edit" Then
      ViewState("mode") = "Edit"
      Field_txt.Text = e.Item.Cells(1).Text
      ViewState("country_cd") = e.Item.Cells(0).Text
      OnMode("Edit")
      Field_txt.Focus()

    ElseIf e.CommandName = "Cities" Then
      Session("country_cd") = e.Item.Cells(0).Text
      Response.Redirect("~/Admin/Add_Cities.aspx")
    End If

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
    DataGrid.CurrentPageIndex = e.NewPageIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "COUNTRY"
    zClass.DataGridFill(DataGrid, Results_lbl)

  End Sub

End Class